package com.zhiyou.controller.HouseController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou.bean.House;
import com.zhiyou.service.HouseServiceImpl.HouseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class HouseController {

    @Autowired
    HouseServiceImpl houseService;

    //获取所有 分页
    @RequestMapping("/getAllHouse")
    public String getAllHouse(@RequestParam(value="pn",defaultValue = "1")Integer pn, Map<String,PageInfo> map
            ,HttpSession session) {
        PageInfo pageInfo=null;
        if( session.getAttribute("f")=="1"){
            PageHelper.startPage(pn,5);
            String field1 = (String) session.getAttribute("field1");
            String keyword1 = (String) session.getAttribute("keyword1");
            List<House> slist = houseService.searchHouse(field1, keyword1);
            pageInfo= new PageInfo(slist,5);
        }else{

            List<House> list=new ArrayList<>();
            PageHelper.startPage(pn,5);
            list=houseService.getAllHose();
            pageInfo= new PageInfo(list,5);
        }
        map.put("list",pageInfo);
        return "houselist";
    }
    //查询
    @RequestMapping("/searchHouse")
    public String Search(String keyword,HttpSession session,String field){
        if(keyword!=""){
            session.setAttribute("f","1");
            session.setAttribute("keyword1",keyword);
            session.setAttribute("field1",field);
        }else{
            session.invalidate();
        }
            
        return "redirect:getAllHouse";
    }
    //假删除
    @RequestMapping("/delHouse")
    public String delHouse(String h_id){
        houseService.delHouse(h_id);


        System.out.println(" = " );
        return "redirect:getAllHouse";
    }

    //详细信息
    @RequestMapping("/detailHouse")
    public String detailHouse(String h_id, Map<String,House> map){
       House house=houseService.getOne(h_id);
        map.put("house",house);
        return "housedetail";
    }
    //编辑页面
    @RequestMapping("/editHouse")
    public String editHouse(String h_id, Map<String,House> map){
        House house = houseService.getOne(h_id);
        map.put("house",house);
        return "houseedit";
    }
    //编辑数据
    @RequestMapping("/editHouse2")
    public String updateHouse(House house, String updatatime){
        System.out.println("house = " + house);
        System.out.println("updatetime = " + updatatime);
        houseService.updateHouse(house,updatatime);
        return "redirect:getAllHouse";
    }

    @RequestMapping("/addHouse")
    public String addHouse(){

        return "houseadd";
    }
    @RequestMapping("/addHouse2")
    public String addHouse(House house,String addTime,String updateTime){
        System.out.println("house = " + house);
        houseService.insertHouse(house,addTime,updateTime);
        return "redirect:getAllHouse";
    }
}
